<?php

use yii\db\Migration;

class m000000_000016_create_user_message extends Migration
{
    public function up()
    {
        $this->createTable('{{%user_message}}', [
            'id'=>$this->primaryKey(), // PK
            'uid'=>$this->integer(), // 用户编号
            'sid'=>$this->integer(), // 系统消息编号
            'from_uid'=>$this->integer(), // 发送人编号（系统消息为空）
            'time'=>$this->integer(), // 发送时间
            'title'=>$this->string(256), // 标题
            'content'=>$this->text(), // 内容
            'status'=>$this->integer(), // 状态
        ]);
        $this->createIndex('fk_user_message_user1_idx', '{{%user_message}}', ['uid']);
        try {
            $this->addForeignKey('fk_user_message_user1', '{{%user_message}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
        }
        $this->createIndex('fk_user_message_system_message1_idx', '{{%user_message}}', ['sid']);
        try {
            $this->addForeignKey('fk_user_message_system_message1', '{{%user_message}}', ['sid'], '{{%system_message}}', ['id']);
        } catch (Exception $e) {
        }
        $this->createIndex('fk_user_message_user2_idx', '{{%user_message}}', ['from_uid']);
        try {
            $this->addForeignKey('fk_user_message_user2', '{{%user_message}}', ['from_uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['message_status', 1, '未读'],
            ['message_status', 9, '已读'],
            ['message_status', 0, '删除'],
        ]);
    }

    public function down()
    {
        $this->delete('{{%key_map}}', ['t'=>'message_status']);
        $this->dropTable('{{%user_message}}');
    }
}
